package org.datascooter.test;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.sql.SQLException;

import org.datascooter.DataScooter;
import org.datascooter.bundle.DSMapper;
import org.datascooter.exception.EntityNotMappedException;
import org.datascooter.extension.DatascooterStarter;
import org.datascooter.test.jpaHard.Address;
import org.datascooter.test.jpaHard.CovertProject;
import org.datascooter.test.jpaHard.Employee;
import org.datascooter.test.jpaHard.GovernmentProject;
import org.datascooter.test.jpaHard.PhoneNumber;
import org.datascooter.test.jpaHard.Project;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Complicated example had been taken from JSR 220
 * 
 * @author nemo
 * 
 */
public class JPATestHard {

	private static Address address1, address2, address3, address4;
	private static CovertProject project1, project2, project3, project4;
	private static Employee emp1, emp2, emp3, emp4;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		DatascooterStarter.startEclipseDefaultPath();
		DataScooter.getDefault().dropAll("", true);
		DSMapper.getBundle(Address.class);
		DSMapper.getBundle(Project.class);
		DSMapper.getBundle(CovertProject.class);
		DSMapper.getBundle(Employee.class);
		DSMapper.getBundle(GovernmentProject.class);
		DSMapper.getBundle(PhoneNumber.class);
		DataScooter.getDefault().verifyTables();
	}

	@Test
	public void testAll() throws EntityNotMappedException, SQLException, IOException {
		createAddresses();
		createCovertProjects();
		createEmployees();
		testList();
		testUpdate();
		testDelete();
	}

	public void createAddresses() throws EntityNotMappedException {
		address1 = new Address();
		address2 = new Address();
		address3 = new Address();
		address4 = new Address();
		DataScooter.getDefault().save(address1);
		DataScooter.getDefault().save(address2);
		DataScooter.getDefault().save(address3);
		DataScooter.getDefault().save(address4);
	}

	public void createCovertProjects() throws EntityNotMappedException {
		project1 = new CovertProject();
		project2 = new CovertProject();
		project3 = new CovertProject();
		project4 = new CovertProject();
		DataScooter.getDefault().save(project1);
		DataScooter.getDefault().save(project2);
		DataScooter.getDefault().save(project3);
		DataScooter.getDefault().save(project4);
	}

	public void createEmployees() {
		emp1 = new Employee();
		emp2 = new Employee();
		emp3 = new Employee();
		emp4 = new Employee();
		DataScooter.getDefault().save(emp1);
		DataScooter.getDefault().save(emp2);
		DataScooter.getDefault().save(emp3);
		DataScooter.getDefault().save(emp4);
	}

	public void testList() throws EntityNotMappedException, SQLException, IOException {
		int size = DataScooter.getDefault().list(Address.class.getName()).size();
		assertTrue("Count of Address:  " + size, size == 4);
		assertTrue((DataScooter.getDefault().list(Employee.class).size() == 4));
		assertTrue((DataScooter.getDefault().list(CovertProject.class).size() == 4));
	}

	public void testUpdate() throws EntityNotMappedException, SQLException, IOException {
		DataScooter.getDefault().update(address1, "street", "Baker St");
		DataScooter.getDefault().update(address2, "street", "Abby road");
		assertTrue(((Address) DataScooter.getDefault().firstById(Address.class, address1.getId())).getStreet().equals("Baker St"));
		assertTrue(((Address) DataScooter.getDefault().firstById(Address.class.getName(), address2.getId())).getStreet().equals("Abby road"));
	}

	public void testDelete() throws EntityNotMappedException, SQLException, IOException {
		DataScooter.getDefault().delete(address1);
		DataScooter.getDefault().delete(Address.class.getName(), "street", "Abby road");
		assertTrue(DataScooter.getDefault().firstById(Address.class, address1.getId()) == null);
		assertTrue(DataScooter.getDefault().first(Address.class, "street", "Abby road") == null);
	}

	@AfterClass
	public static void tearDown() throws Exception {
		DataScooter.getDefault().dropAll("", true);
		DataScooter.getDefault().close();
	}
}
